﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Castle.Services.Transaction;
using Castle.Facilities.IBatisNetIntegration;

using Meqs.Entity;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    [Transactional]
    [UsesAutomaticSessionCreation]
    public class CauseInfoLogic : CommonLogic<CauseInfo, int>, ICauseInfoLogic
    {

        #region Implementation of ICommonLogic<CauseInfo, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(CauseInfo entity)
        {
            CreateObject(entity);
            return entity.RID;
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(CauseInfo entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public CauseInfo FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<CauseInfo> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<CauseInfo> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        /// <sammury>
        /// 查找课程的使用人数
        /// wl 2009.10.19 15:00
        /// </sammury>
        public int FindCauseUserCount(int causeid, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("CauseID", causeid);
            ht.Add("SchoolMark", schoolMark);

            try
            {
                return ExecuteQueryForObject<int>("CauseInfo.FindCauseUserCount", ht);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        //yb
        public void InsertCause(CauseInfo cause)
        {
            try
            {
                InsertCustomer("CauseInfo.Insert", cause);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <sammury>
        /// 修改学校的课程设置
        /// wl 2009.10.19 15:00
        /// </sammury>
        [Transaction(TransactionMode.Requires)]
        public int ModifyCause(List<int> addCauses, List<int> delCauses, string strSchoolMark)
        {
            try
            {
                for (int i = 0; i < addCauses.Count; i++)
                {
                    CauseInfo cs = new CauseInfo(addCauses[i], strSchoolMark);

                    InsertCustomer("CauseInfo.Insert",cs);

                }

                for (int j = 0; j < delCauses.Count; j++)
                {
                    CauseInfo cs = new CauseInfo(delCauses[j], strSchoolMark);

                    int iResult = ExecuteDelete("CauseInfo.DeleteCause", cs);
                    if (iResult < 0)
                        return -1;
                }
            }
            catch
            {
                throw;
            }

            return 1;
        }
        public int DeleteById(int causeid, string strSchoolMark)
        {
            try
            {
                CauseInfo cs = new CauseInfo(causeid, strSchoolMark);
                int iResult = ExecuteDelete("CauseInfo.DeleteCause", cs);
                if (iResult < 0)
                    return -1;

            }
            catch
            {
                throw;
            }

            return 1;
        }

        #endregion
    }
}